package com.iretailer.report.web.business;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.iretailer.report.base.BaseAction;
import com.iretailer.report.dto.PageResult;
import com.iretailer.report.dto.QueryOption;
import com.iretailer.report.dto.ReCoefficient;
import com.iretailer.report.model.Coefficient;
import com.iretailer.report.model.CoefficientZone;
import com.iretailer.report.model.User;
import com.iretailer.report.util.RMap;
import com.iretailer.report.util.SecurityUserUtil;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


/**
 * @author Bennie
 */
@RestController
public class CoefficientController extends BaseAction {

    @RequestMapping("/coefficient/gridCameraCoefficient.action")
    public Map<String, Object> gridCameraCoefficient(@RequestBody ReCoefficient rbt, HttpServletRequest request) {
        QueryOption queryOption = modelMapper.map(rbt, QueryOption.class);
         User user = SecurityUserUtil.currentUser(request);
        List<String> companyIds = null;
        if (!"-".equals(user.getRoleId())) {
            companyIds = companyService.getTreeId(user.getCompanyId());
            companyIds.add(user.getCompanyId());
        }

        PageResult find = coefficientService.gridCoefficient(companyIds, rbt.getCompanyName(), queryOption);
        return RMap.ok(find);
    }

    @RequestMapping("/coefficient/gridMallCameraCoefficient.action")
    public Map<String, Object> gridMallCameraCoefficient(@RequestBody ReCoefficient rbt,HttpServletRequest request) {
        QueryOption queryOption = modelMapper.map(rbt, QueryOption.class);
         User user = SecurityUserUtil.currentUser(request);
        String userId = user.getId();
        if ("-".equals(user.getRoleId())) {
            userId = null;
        }
        PageResult find = coefficientZoneService.gridZoneCoefficient(userId, rbt.getCompanyName(), queryOption);
        return RMap.ok(find);
    }

    @RequestMapping("/coefficient/saveOrUpdateCameraCoefficient.action")
    public Map<String, Object> saveOrUpdateCameraCoefficient(@RequestBody ReCoefficient rbt) {
        Coefficient coefficient = modelMapper.map(rbt, Coefficient.class);
        boolean result = coefficientService.saveOrUpdate(coefficient);
        return RMap.resultBoolean(result);
    }

    @RequestMapping("/coefficient/saveOrUpdateMallCameraCoefficient.action")
    public Map<String, Object> saveOrUpdateMallBusinessTime(@RequestBody ReCoefficient rbt) {
        // 查询此营业时间关联案场
        List<CoefficientZone> coefficientZones = coefficientZoneService.list(new QueryWrapper<CoefficientZone>().lambda().eq(CoefficientZone::getCoefficientId, rbt.getCoefficientId()));
        Coefficient coefficient = modelMapper.map(rbt, Coefficient.class);
        // 如果只有一个案场 则更新营业时间信息
        boolean result;
        if (coefficientZones.size() == 1) {
            coefficient.setId(rbt.getCoefficientId());
            result = coefficientService.saveOrUpdate(coefficient);
        } else {
            // 如果有多个则新建一个
            result = coefficientService.creatCoefficient(coefficient, rbt.getCoefficientId(), rbt.getZoneId());
        }
        return RMap.resultBoolean(result);
    }


    @RequestMapping("/coefficient/findZoneIdsByCoefficientId.action")
    public Map<String, Object> findZoneIdsByCoefficientId(@RequestBody ReCoefficient reCoefficient) {
        return RMap.ok(coefficientService.findZoneIdsByCoefficientId(reCoefficient.getCoefficientId()));
    }

    @RequestMapping("/coefficient/deleteCameraCoefficient.action")
    public Map<String, Object> deleteCameraCoefficient(@RequestBody List<Long> ids) {
        boolean result = coefficientService.deleteCoefficientByIds(ids);
        return RMap.resultBoolean(result);
    }



    @RequestMapping("/coefficient/cameraCoefficientSaveMalls.action")
    public Map<String, Object> cameraCoefficientSaveMalls(@RequestBody ReCoefficient rbt) {
        boolean result = coefficientService.saveCoefficientZones(rbt.getId(), rbt.getZoneIds());
        return RMap.resultBoolean(result);
    }


    @RequestMapping("/coefficient/deleteCameraCoefficientByIds.action")
    public Map<String, Object> deleteCameraCoefficientByIds(@RequestBody List<String> ids) {
        boolean result = coefficientService.deleteByIds(ids);
        if (result) {
            return RMap.ok();
        } else {
            return RMap.error(RMap.Code.CONNECTED, "associated_with_mall");
        }
    }

    @RequestMapping("/coefficient/selectCompanyCoefficient.action")
    public Map<String, Object> selectCompanyCoefficient(@RequestBody ReCoefficient reCoefficient) {
        List<Coefficient> coefficients = coefficientService.selectByCompanyId(reCoefficient.getCompanyId());
        return RMap.ok(coefficients);
    }
}
